Completed
Push — master ( 1fc2c9...675e71 )
by greg
01:46
created

metas.js ➔ add   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
c 1
b 0
f 0
nc 8
dl 0
loc 23
rs 5.6534
nop 6

How to fix   Complexity   

Complexity

Complex classes like metas.js ➔ add often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {
2
  cmsData
3
  ,config
4
} from '../../'
5
6
export function add(tpl, json, type, obj = {}, date = null, realType = 'draft') {
7
  let meta = config.meta.name
8
9
  var currentDate = (typeof date !== 'undefined' && date !== null && date !== '') ? date : new Date()
10
  var abeUrl = (type === 'publish') ? json[meta].link : cmsData.fileAttr.add(json[meta].link, 'd' + cmsData.revision.removeStatusAndDateFromFileName(currentDate.toISOString())) + ''
11
12
  if(typeof json[meta].date === 'undefined' || json[meta].date === null) {
13
    json[meta].date = currentDate
14
  }
15
  json[meta].latest = {
16
    date: currentDate,
17
    abeUrl: abeUrl
18
  }
19
  json[meta].status = realType === 'reject' ? 'draft' : realType
20
  if(typeof json[meta][type] === 'undefined' || json[meta][type] === null) {
21
    json[meta][type] = JSON.parse(JSON.stringify(obj))
22
    json[meta][type].date = currentDate
23
    json[meta][type].abeUrl = abeUrl
24
  }
25
  json[meta][type].latest = JSON.parse(JSON.stringify(obj))
26
  json[meta][type].latest.date = currentDate
27
  json[meta][type].latest.abeUrl = abeUrl
28
}
29
30
export function get(arr) {
31
  var res = []
32
  Array.prototype.forEach.call(arr, (file) => {
33
    let meta = config.meta.name
34
35
    var jsonPath = cmsData.file.fromUrl(file.path).json.path
36
    var json = cmsData.file.get(jsonPath)
37
    if(typeof json[meta] === 'undefined' || json[meta] === null) json[meta] = {}
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
38
    file['template'] = json[meta].template
39
    if(typeof json[meta].latest !== 'undefined' && json[meta].latest !== null) {
40
      file['date'] = json[meta].latest.date
41
    }
42
    if(typeof json[meta].complete === 'undefined' || json[meta].complete === null) {
43
      json[meta].complete = 0
44
    }
45
    if(typeof json[meta] !== 'undefined' && json[meta] !== null) {
46
      file[config.meta.name] = json[meta]
47
    }
48
    res.push(file)
49
  })
50
51
  return res
52
}